//
//  CYChooseCouponCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/9.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYChooseCouponCell.h"

#import "CYDottedLine.h"



@implementation CYChooseCouponCellObject

@end



@interface CYChooseCouponCell ()

@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UIImageView *backgroundImageView;
@property(nonatomic, strong)UILabel *descLabel;
@property(nonatomic, strong)UILabel *unitLabel;
@property(nonatomic, strong)UILabel *amountLabel;
@property(nonatomic, strong)UILabel *dueLabel;
@property(nonatomic, strong)UILabel *warningLabel;
@property(nonatomic, strong)UILabel *dateLabel;
@property(nonatomic, strong)UIImageView *selectImageView;
@property(nonatomic, strong)UIImageView *tagImageView;

@property(nonatomic, strong)CYDottedLine *line;
@property(nonatomic, strong)UILabel *expandDescLabel;
@property(nonatomic, strong)UIImageView *expandIcon;
@property(nonatomic, strong)UIButton *expandButton;

@property(nonatomic, strong)UIButton *expandView;
@property(nonatomic, strong)UIImageView *expandBgView;
@property(nonatomic, strong)UILabel *expandLabel;

@property(nonatomic, strong)CYChooseCouponCellObject *object;

@end


@implementation CYChooseCouponCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        self.expandView = [[UIButton alloc] init];
        [self.contentView addSubview:self.expandView];
        {
            self.expandBgView = [[UIImageView alloc] initWithImage:CYImage(@"coupon_shadow")];
            [self.expandView addSubview:self.expandBgView];
            
            self.expandLabel = [[UILabel alloc] init];
            self.expandLabel.textColor = HEXCOLOR(@"999999");
            self.expandLabel.font = [UIFont systemFontOfSize:10];
            self.expandLabel.numberOfLines = 0;
            [self.expandView addSubview:self.expandLabel];
        }
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 10;
        [self.contentView addSubview:self.bgView];
        
        self.backgroundImageView = [[UIImageView alloc] init];
        [self.bgView addSubview:self.backgroundImageView];
        
        self.descLabel = [[UILabel alloc] init];
        self.descLabel.textColor = HEXCOLOR(@"ffffff");
        self.descLabel.font = [UIFont systemFontOfSize:30 weight:UIFontWeightBold];
        [self.contentView addSubview:self.descLabel];
        
        self.unitLabel = [[UILabel alloc] init];
        self.unitLabel.textColor = HEXCOLOR(@"ffffff");
        self.unitLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
        [self.contentView addSubview:self.unitLabel];
        
        self.amountLabel = [[UILabel alloc] init];
        self.amountLabel.textColor = HEXCOLOR(@"ffffff");
        self.amountLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.amountLabel];
        
        self.dueLabel = [[UILabel alloc] init];
        self.dueLabel.font = [UIFont systemFontOfSize:12];
        self.dueLabel.textColor = HEXCOLOR(@"666666");
        [self.contentView addSubview:self.dueLabel];
        
        self.warningLabel = [[UILabel alloc] init];
        self.warningLabel.textColor = APP_DEFAULT_COLOR_RED;
        self.warningLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:self.warningLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.textColor = HEXCOLOR(@"999999");
        self.dateLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:self.dateLabel];
        
        self.selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        [self.contentView addSubview:self.selectImageView];
        
        self.tagImageView = [[UIImageView alloc] initWithImage:CYImage(@"coupon_tag_deadline")];
        [self.contentView addSubview:self.tagImageView];
        
        self.line = [[CYDottedLine alloc] init];
        self.line.backgroundColor = [UIColor clearColor];
        self.line.lineWidth = 1 / SCREEN_SCALE;
        self.line.lineColor = HEXCOLOR(@"E9E9E9");
        self.line.lengths = @[@1, @1];
        self.line.phase = 0;
        [self.contentView addSubview:self.line];
        
        self.expandDescLabel = [[UILabel alloc] init];
        self.expandDescLabel.text = @"点击查看适用标的";
        self.expandDescLabel.textColor = HEXCOLOR(@"999999");
        self.expandDescLabel.font = [UIFont systemFontOfSize:10];
        [self.expandDescLabel sizeToFit];
        [self.contentView addSubview:self.expandDescLabel];
        
        self.expandIcon = [[UIImageView alloc] init];
        [self.contentView addSubview:self.expandIcon];
        
        self.expandButton = [[UIButton alloc] init];
        [self.expandButton addTarget:self action:@selector(onExpandButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.expandButton];
    }
    return self;
}

+ (CGFloat)heightForObject:(CYChooseCouponCellObject *)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    CGFloat expandHeight = 0;
    if(object.expand)
    {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:10];
        label.numberOfLines = 0;
        label.text = [NSString stringWithFormat:@"适用标的：%@", object.model.canBuyProject];
        expandHeight = [label sizeThatFits:CGSizeMake(tableView.width - 80, MAXFLOAT)].height + 30;
    }
    return 125 + expandHeight;
}

- (BOOL)shouldUpdateCellWithObject:(CYChooseCouponCellObject *)object
{
    self.object = object;
    
    self.descLabel.text = SAFE_STRING(object.model.vcFinalAmount);
    self.unitLabel.text = SAFE_STRING(object.model.couponUnit);
    self.amountLabel.text = [NSString stringWithFormat:@"满%@元可用", SAFE_NUMBER(object.model.damountLowerLimit).stringValue];
    self.dueLabel.text = [NSString stringWithFormat:@"产品期限: ≥%@天", SAFE_NUMBER(object.model.idayLowerLimit).stringValue];
    self.warningLabel.text = [NSString stringWithFormat:@"不适用原因: %@", SAFE_STRING(object.model.vcremark)];
    self.dateLabel.text = [NSString stringWithFormat:@"有效期至: %@", [[SAFE_NUMBER(object.model.dtExpireTime) cy_date] cy_stringWithFormat:@"yyyy-MM-dd"]];
    self.selectImageView.image = object.selected ? CYImage(@"coupon_selected") : CYImage(@"coupon_unselected");
    self.tagImageView.hidden = SAFE_NUMBER(object.model.expiring).integerValue != 3;
    
    self.expandIcon.image = object.expand ? CYImage(@"ico_switch_arrow_top") : CYImage(@"ico_switch_arrow_bottom");
    self.expandLabel.text = [NSString stringWithFormat:@"适用标的：%@", SAFE_STRING(object.model.canBuyProject)];
    self.expandView.hidden = !object.expand;
    
    if(SAFE_NUMBER(object.model.iUse).integerValue == 1)
    {
        self.backgroundImageView.image = CYImage(@"coupon_bg");
        self.warningLabel.hidden = YES;
        self.selectImageView.hidden = NO;
    }
    else
    {
        self.backgroundImageView.image = CYImage(@"coupon_bg_disable");
        self.warningLabel.hidden = NO;
        self.selectImageView.hidden = YES;
    }
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.bgView.width = self.contentView.width - 40;
    self.bgView.height = 115;
    self.bgView.centerX = self.contentView.width / 2;
    self.bgView.top = 10;
    
    [self.backgroundImageView sizeToFit];
    self.backgroundImageView.left = 0;
    self.backgroundImageView.top = 0;
    
    [self.descLabel sizeToFit];
    self.descLabel.centerY = self.bgView.top + 48;
    [self.unitLabel sizeToFit];
    self.unitLabel.centerY = self.bgView.top + 48;
    CGFloat width = self.descLabel.width + self.unitLabel.width;
    CGFloat left = 82.5 - width / 2;
    self.descLabel.left = left;
    self.unitLabel.left = self.descLabel.right;
    
    [self.amountLabel sizeToFit];
    self.amountLabel.centerX = 82.5;
    self.amountLabel.centerY = self.bgView.top + 71.5;
    
    [self.dueLabel sizeToFit];
    self.dueLabel.left = 160;
    self.dueLabel.centerY = self.bgView.top + 24;
    
    [self.warningLabel sizeToFit];
    self.warningLabel.left = 160;
    self.warningLabel.centerY = self.bgView.top + 57;
    
    [self.dateLabel sizeToFit];
    self.dateLabel.left = 160;
    self.dateLabel.centerY = self.bgView.top + 73;
    
    self.selectImageView.top = 15;
    self.selectImageView.right = self.contentView.width - 30;
    
    self.tagImageView.left = self.bgView.left;
    self.tagImageView.top = self.bgView.top;
    
    self.line.height = 5;
    self.line.width = self.contentView.width - 190;
    self.line.left = 160;
    self.line.top = self.bgView.top + 84;
    self.line.startPoint = CGPointMake(0, self.line.bounds.size.height / 2);
    self.line.endPoint = CGPointMake(self.line.bounds.size.width, self.line.bounds.size.height / 2);
    [self.line setNeedsDisplay];
    
    self.expandDescLabel.left = 160;
    self.expandDescLabel.centerY = self.bgView.top + 100;
    
    [self.expandIcon sizeToFit];
    self.expandIcon.right = self.contentView.width - 30;
    self.expandIcon.top = self.bgView.top + 93;
    
    self.expandButton.width = self.line.width;
    self.expandButton.height = 29;
    self.expandButton.left = self.line.left;
    self.expandButton.top = self.line.bottom;
    
    self.expandView.width = self.bgView.width;
    self.expandView.height = self.contentView.height - self.bgView.bottom + 5;
    self.expandView.left = self.bgView.left;
    self.expandView.top = self.bgView.bottom - 10;
    {
        self.expandBgView.frame = self.expandView.bounds;
        
        self.expandLabel.size = [self.expandLabel sizeThatFits:CGSizeMake(self.expandView.width - 40, MAXFLOAT)];
        self.expandLabel.left = 20;
        self.expandLabel.bottom = self.expandView.height - 15;
    }
}

#pragma mark - Control Events
- (void)onExpandButtonTouchUpInside
{
    self.object.expand = !self.object.expand;
    if(self.object.expandAction)
    {
        self.object.expandAction();
    }
}

@end
